:Init

@setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@pushd %~dp0
@prompt $_
@echo on

if "%1" == "" goto Usage
if "%2" == "" goto Usage

if "%DiagnosticsLogs%" == "" set DiagnosticsLogs=%TEMP%
set _dinput=%DiagnosticsLogs%\accelerator.setUmbracoPermissions.log
set _dlog=%DiagnosticsLogs%\accelerator.setUmbracoPermissions.log 
echo %_dlog% > "%_dlog%" 2>&1
echo Application Pool:  %1 >> "%_dlog%" 2>&1
echo Root Folder:       %2 >> "%_dlog%" 2>&1 
goto CheckPaths

:Usage
@echo.
@echo Secures an Umbraco site with application pool specific security settings.
@echo.
@echo SETUMBRACOPERMISSIONS appPoolName [drive:][path]filename
@echo.
@echo   appPoolName Specifies the application pool  name to use when setting folder
@echo               security settings.
@echo.
@echo   [drive:][path][filename]
@echo               Specifies the root folder of an Umbraco web site installation.
@echo.
@goto End

:CheckPaths
if not exist "%2" (
    @echo Umbraco site not found at '%2'. >> "%_dlog%" 2>&1
    @goto End
)
cd /d %2 >> "%_dlog%" 2>&1 
dir /q /l /n >> "%_dlog%" 2>&1 


:SetPermissions
icacls . /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
icacls app_code /grant "IIS APPPOOL\%1":(OI)(CI)RX >> "%_dlog%" 2>&1
icacls app_browsers /grant "IIS APPPOOL\%1":(OI)(CI)RX >> "%_dlog%" 2>&1
icacls app_data /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
icacls bin /grant "IIS APPPOOL\%1":(OI)(CI)R >> "%_dlog%" 2>&1
icacls config /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
icacls css /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
icacls data /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
icacls masterpages /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
icacls media /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
icacls python /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
icacls scripts /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
icacls umbraco /grant "IIS APPPOOL\%1":(OI)(CI)R >> "%_dlog%" 2>&1
icacls usercontrols /grant "IIS APPPOOL\%1":(OI)(CI)R >> "%_dlog%" 2>&1
icacls xslt /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
icacls web.config /grant "IIS APPPOOL\%1":(OI)(CI)M >> "%_dlog%" 2>&1
type "%_dlog%"

:Completed
@echo.
@echo Completed.
@echo.
:End